package friot.service.cloud.controller;

import friot.model.iot.dtos.IotDeviceDto;
import friot.model.iot.vo.IotDeviceVo;
import friot.service.cloud.service.IotDeviceService;
import friot.utils.tools.JwtUtil;
import friot.utils.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.spi.http.HttpHandler;
import java.util.List;

/**
 * @author He Kuntao
 * @desc TODO
 * @date 2024/3/13 23:06
 */
@Slf4j
@RestController
@RequestMapping("/api/iot/device")
public class IotDeviceController {

    @Resource
    private IotDeviceService iotDeviceService;

    /**
     * 获取当前用户创建的设备列表
     *
     * @return Result
     */
    @GetMapping
    public Result listDevice(HttpServletRequest request) {
        int userId = JwtUtil.parseTokenToUserId(request);
        List<IotDeviceVo> iotDeviceDto = iotDeviceService.listAndMapByUserId(userId);
        return Result.success(iotDeviceDto);
    }


    /**
     * 创建设备
     *
     * @param deviceDto 设备DTO
     * @return Result
     */
    @PostMapping
    public Result createDevice(@RequestBody IotDeviceDto deviceDto) {
        iotDeviceService.addDevice(deviceDto);
        return Result.success();
    }
}
